//var $j = jQuery.noConflict();
/* uncomment this block if there are some conflicts with other js library/framework */
(function ($) {

MyPage = {
    element: {
        btnSearch: null
		, providerList:null
		, frmSearch:null
		, frmProviderList:null
		, txtSearch:null
    },

    builder: {
        createProviderLink: function ( id , name ) {
			return $('<div></div>')
				.append(
					$('<a></a>').addClass('lnkProvider').attr('title', id ).attr('href', 'javascript:void(0)')
						.text(name)
			) ;
        }
	},

    application: {
		resetForm: function ( ){
			MyPage.element.frmSearch.find('input:checkbox[name="chkServiceType[]"]').attr('checked' , false ).attr('selected' , false );
		},
		
		refreshProviderList: function ( data ){
			MyPage.element.providerList.empty(); 
			if ( data.providerList != null )
			{
				$.each(data.providerList , function() {
					MyPage.element.providerList.append (
						MyPage.builder.createProviderLink ( this.id , this.name ).html() + "<BR/>"
					);
				});
			}
			
		},
		listProvider: function ( url , searchType , searchText ){
			//window.location.href = url;
			var data = { searchType : searchType , searchText : searchText }
            AIC.Ajax.ajaxCall (url, "POST" , data , function (data) {
                if (data.status === true) {
                    MyPage.application.refreshProviderList ( data );
                } else {
					App.alert("Error Retrieving Data From Server" , "Error");
                }
            });
		},
		viewProvider: function ( url , id ){
			window.location.href = url;
			var data = { searchType : searchType , searchText : searchText }
            AIC.Ajax.ajaxCall (url, "POST" , data , function (data) {
                if (data.status === true) {
                    MyPage.application.refreshProviderList ( data );
                } else {
					App.alert("Error Retrieving Data From Server" , "Error");
                }
            });
		}
    },

    prepareElement: function () {
        this.element = {
            btnSearch: $("#btnSearch")
			, providerList: $("#providerList")
			, frmSearch: $("#frmSearch")
			, frmProviderList: $("#frmProviderList")
			, txtSearch: $("#txtSearch")
        };
    },

    prepareView: function () {
        this.element.btnSearch.button();
    },

    eventBinding: function () {
        this.element.btnSearch.click(function () {
			var url = MyPage.element.frmSearch.attr('action') ;
			var searchType = "Keyword";
			var searchText = MyPage.element.txtSearch.val();
			MyPage.application.listProvider ( url , searchType , searchText );
			MyPage.element.txtSearch.focus();
			return false;
        });
        this.element.frmSearch.submit( function () {
			return false;
        });
        this.element.frmProviderList.submit( function () {
			/////return false;
        });
        this.element.txtSearch.bind("keydown", function (event) {
            if (event.keyCode === $.ui.keyCode.ENTER) {
				MyPage.element.btnSearch.click();
            }
        });
        this.element.frmSearch.delegate('.lnkAlphabeth', 'click', function () {
			var url = MyPage.element.frmSearch.attr('action') ;
			var searchType = "Alphabeth";
			var searchText =  $(this).attr("title");
			MyPage.application.listProvider ( url , searchType , searchText );
        });
		this.element.frmProviderList.delegate('.lnkProvider', 'click', function () {
			var id = $(this).attr("title");
			$("#hidId").val(id);
			MyPage.element.frmProviderList.submit();
        });

    },

    main: function () {
        this.prepareElement();
        this.prepareView();
        this.eventBinding();
    }
};

$(function () {
    MyPage.main();
});

/* uncomment this block if there are some conflicts with other js library/framework */
})(jQuery);


